import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;

import com.jgoodies.forms.factories.DefaultComponentFactory;

/*
 * MailGUI.java - This class gets information from user and sends them to Mail class which 
 * 					connects the web to the program. Therefore, this class is the graphical 
 * 					representation of Mail class.
 * 
 * @Author : BilCIS
 */

public class MailGUI extends JFrame
{	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7217578503100340204L;
	
	// Variables declaration
	private	String to;
	private	String subject;
	private	String message;
	
	private boolean flag = false;
	
	private JButton sendButton;
	private JButton exit;
	private JButton companyButton;
	private JButton connectToGmail;
	private	JButton attachFile;
	
	private JPanel contentPane;
		
	private	JLabel lblTo;
	private	JLabel lblSubject;
	private	JLabel tick;
	
	private JTextArea toTF;
	private JTextArea subjectTF; 
	private JTextArea messageTF; 
	
	private String existingMessage;
	private String existingSubject;
	private String existingTo;
	
	
	private String username;
	private String password;
	
	private	KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0);	

	private AbstractAction action;
	
	static Point mouseDownCompCoords = null;
	
	private	File file;
	private Employee emp;
	private Mysql 	mysql;
	private	Color color;
	// End of variables declaration
	
	public MailGUI( Employee emp )
	{
		this.emp = emp;
		this.username = emp.getGmail_username();
		this.password = emp.getGmail_password();
		existingMessage = "";
		existingSubject = "";
		existingTo = "";
		create();
		this.setVisible(true);
	}
	
	public MailGUI( Employee emp, String existingMessage, String existingSubject )
	{
		this.emp = emp;
		this.username = emp.getGmail_username();
		this.password = emp.getGmail_password();
		existingTo = "";
		this.existingMessage = existingMessage;
		this.existingSubject = existingSubject;
		create();
		this.setVisible(true);
	}
	
	public MailGUI( Employee emp, String existingTo, String existingMessage, String existingSubject )
	{
		this.emp = emp;
		this.existingTo = existingTo;
		this.existingMessage = existingMessage;
		this.existingSubject = existingSubject;
		create();
		this.setVisible(true);
	}
	
	public MailGUI( Color color, String username, String password, String existingTo, String existingMessage, String existingSubject )
	{
		this.color = color;
		this.username = username;
		this.password = password;
		this.existingTo = existingTo;
		this.existingMessage = existingMessage;
		this.existingSubject = existingSubject;
		create();
		this.setVisible(true);
	}
	
	public void create()
	{
		//
		//	Create controls
		//
		contentPane = new JPanel(null);
		toTF = new JTextArea(existingTo);
		subjectTF = new JTextArea(existingSubject);
		messageTF = new JTextArea(existingMessage);
		sendButton  = new JButton("Send");
		exit = new JButton("Exit");
		lblTo = DefaultComponentFactory.getInstance().createLabel("To:");
		lblSubject = DefaultComponentFactory.getInstance().createLabel("Subject:");
		companyButton = new JButton("Company");
		connectToGmail = new JButton("Connect Gmail");
		attachFile = new JButton("Attach a File");
		tick = new JLabel("");
		//
		//Register action listeners
		//
		exit.addActionListener(new exit_Action());
		sendButton.addActionListener(new sendButton_Action());
		companyButton.addActionListener(new companyButton_Action());
		connectToGmail.addActionListener(new connectToGmail_Action());
		attachFile.addActionListener(new attachFile_Action());
		//	
		// Mnemonics
		//
		exit.setMnemonic('e');
		sendButton.setMnemonic('s');
		companyButton.setMnemonic('c');
		connectToGmail.setMnemonic('g');
		attachFile.setMnemonic('a');
		//
		//	Add controls to pane
		//
		this.add(contentPane);	
		contentPane.add(toTF);
		contentPane.add(lblTo);
		contentPane.add(subjectTF);
		contentPane.add(lblSubject);
		contentPane.add(messageTF);
		contentPane.add(sendButton);
		contentPane.add(exit);
		contentPane.add(companyButton);
		contentPane.add(connectToGmail);
		contentPane.add(attachFile);
		contentPane.add(tick);
		//	
		//	Set bounds
		//
		contentPane.setBounds(0,0,700,500);
		lblTo.setBounds(10, 80, 70, 25);
		toTF.setBounds(90,80,600,25);
		lblSubject.setBounds(10, 115, 70, 25);
		subjectTF.setBounds(90, 115, 600, 25);
		messageTF.setBounds(20, 200, 670, 240);
		sendButton.setBounds(300, 450, 100, 30);
		exit.setBounds(580, 15, 110, 50);
		companyButton.setBounds (50, 15, 150, 50);
		connectToGmail.setBounds (270, 15, 180, 50);
		attachFile.setBounds (280, 143, 170, 50);
		tick.setBounds(472, 150, 35, 35);
		//	
		//	Setting Icons
		//
		exit.setIcon(new ImageIcon( new File("exit.png").getAbsolutePath() ));
		tick.setVisible(false);
		tick.setIcon(new ImageIcon( new File("tick.png").getAbsolutePath() ));
		companyButton.setIcon(new ImageIcon( new File("companyMail.png").getAbsolutePath() ));
		connectToGmail.setIcon(new ImageIcon( new File("gmail.jpg").getAbsolutePath() ));
		attachFile.setIcon(new ImageIcon( new File("attachment.jpg").getAbsolutePath() ));
		//
		//	to label
		//
		lblTo.setForeground(new Color(0, 100, 0));
		lblTo.setFont(new Font("Comic Sans MS", Font.BOLD, 16));
		//
		//	subject label
		//
		lblSubject.setForeground(new Color(0, 100, 0));
		lblSubject.setFont(new Font("Comic Sans MS", Font.BOLD, 16));
		//********************** Colors *********************//
		if (emp != null)
			contentPane.setBackground(emp.getColor());
		if (color != null)
			contentPane.setBackground(color);
		//
		//	exit KEY action
		//
		action = new AbstractAction(){
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public void actionPerformed (ActionEvent e)
			{
				dispose();
			}
		};
		contentPane.setInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, contentPane.getInputMap());
		contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(key, "pressed");
		contentPane.getActionMap().put("pressed", action);
		//
		// MailGUI
		//
		Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
		this.setIconImage(Toolkit.getDefaultToolkit().getImage(new File("mail.png").getAbsolutePath()));
		this.setLocation(scrSize.width/2, scrSize.height/4 );
		this.setUndecorated(true);
		this.setSize(new Dimension(700, 500));
		this.setLayout(null);
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		contentPane.setBorder(BorderFactory.createTitledBorder("Mail"));
		// mouse cursor with undecorated
		this.addMouseListener(new MouseListener()
		{
		    public void mouseReleased(MouseEvent e) 
		    {
		        mouseDownCompCoords = null;
		    }
		    public void mousePressed(MouseEvent e) 
		    {
		        mouseDownCompCoords = e.getPoint();
		    }
		    public void mouseExited(MouseEvent e) {}
		    public void mouseEntered(MouseEvent e) {}
		    public void mouseClicked(MouseEvent e) {}
		});
		this.addMouseMotionListener(new MouseMotionListener()
		{
		    public void mouseMoved(MouseEvent e) {}
		    public void mouseDragged(MouseEvent e) 
		    {
			    Point currCoords = e.getLocationOnScreen();
			    setLocation(currCoords.x - mouseDownCompCoords.x, currCoords.y - mouseDownCompCoords.y);
		    }
		});
		// end of mouse cursor
	}
	
	// attach file button
	public class attachFile_Action implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			JFileChooser chooser = new JFileChooser();
			int status = chooser.showOpenDialog(null);
			
			if(status != JFileChooser.APPROVE_OPTION)
				JOptionPane.showMessageDialog(null,"No File Chosen");
			else
			{
				file = chooser.getSelectedFile();
				tick.setVisible(true);
				flag = true;
			}
		}
	}
	
	// opens connect
	public class connectToGmail_Action implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			try {
				java.awt.Desktop.getDesktop().browse(java.net.URI.create("http://gmail.com"));
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	// gets all company member's e-mails 
	public class companyButton_Action implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			mysql = new Mysql(emp.getID());
			try {
				toTF.setText( mysql.getCompanyMails() );
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	// sends the mail
	public class sendButton_Action implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			Music sendingemail = new Music( new File("sendingemail.wav").getAbsolutePath() );
			sendingemail.play();
			
			to = new String(toTF.getText());
			subject = new String(subjectTF.getText());
			message = new String(messageTF.getText());
				
				
			Mail m = new Mail (username,password);
			m.setMessage( message );
			m.setSubject( subject );
			if ( flag == true )
				m.setAttachment(file.getAbsolutePath(),file.getName());
			m.send( to );
		}
	}
	
	// disposes the frames
	public class exit_Action implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			dispose();
		}
	}
}
